package com.example.demo.practice.devices;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.MapAndList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class InspectionHistory extends IBaseActivity {
    private MapAndList mapAndList;
    private List<String> geoJson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_history);
        setTitle("巡检记录");

        mapAndList = (MapAndList) findViewById(R.id.mapAndList);
        mapAndList.loadUrl("html/devices/Inspection.html");
        serverHttp=new ServerHttp(new UpdateList());
        serverHttp.setUrl("getInspection");
    }

    class UpdateList extends Handler{
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray=serverHttp.getData();
            geoJson=new ArrayList<>();
            List<Map<String, Object>> groupArray = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    map.put("worker", json.getString("worker"));
                    String Time = json.getString("date");
                    String[] AuditDates = Time.split("T");
                    map.put("date", AuditDates[0]);
                    map.put("back", json.getString("back"));
                    geoJson.add(json.getString("st_asgeojson"));
                    groupArray.add(map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jsonArray = null;
            serverHttp.clear();
            //适配列表

            SimpleAdapter simpleAdapter = new SimpleAdapter(InspectionHistory.this,
                    groupArray,
                    R.layout.item_inspection,
                    new String[]{"worker", "back", "date"},
                    new int[]{R.id.worker, R.id.back, R.id.date});
            mapAndList.setListAdapter(simpleAdapter);
            mapAndList.setItemClickListener(new SingleClick());
            super.handleMessage(msg);
        }

    }

    public class SingleClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String json = geoJson.get(i);
            Log.e("ss",json);
            mapAndList.drawLine(json);
        }
    }
}
